/*
 * JobPoll.cpp
 *
 *   Created on: 2010-3-30     
 *  Description: 
 *    Copyright: 2010 @ ICT Li Heyuan
 */

#include "../../include/thread/JobPoll.h"

JobPoll::JobPoll()
{
	//默认Fast类型的mutex
	pthread_mutex_init(&_mutex_jobs, NULL);
	//创建默认的cond
	pthread_cond_init(&_cond_new_job, NULL);
}

JobPoll::~JobPoll()
{
	pthread_mutex_destroy(&_mutex_jobs);
}

void JobPoll::addJob(Conn *conn)
{
	pthread_mutex_lock(&_mutex_jobs);

	_jobs.push_back(conn);

	pthread_mutex_unlock(&_mutex_jobs);
}

void JobPoll::thread_func_worker(void *arg)
{
	Job job;
	while (true)
	{
		//等待直到有任务需要做
		pthread_mutex_lock(&_mutex_jobs);
		while (_jobs.empty())
		{
			pthread_cond_wait(&_cond_new_job, &_mutex_jobs);
		}
		//获取Job
		job = _jobs.front();
		_jobs.pop_front();
		pthread_mutex_unlock(&_mutex_jobs);

		//执行Job
	}
}

void JobPoll::thread_func_master(void *arg)
{
	Job job;
	while (true)
	{
		//等待直到有任务需要做
		pthread_mutex_lock(&_mutex_jobs);
		while (_jobs.empty())
		{
			pthread_cond_signal(&_cond_new_job, &_mutex_jobs);
		}
		//获取Job
		job = _jobs.front();
		_jobs.pop_front();
		pthread_mutex_unlock(&_mutex_jobs);

		//执行Job
	}
}
